package com.wishtree.common;

import lombok.Data;

import java.util.List;
import java.util.Objects;
@Data
public class ResultBody<T> {
    private String code;
    private String msg;
    private T data;

    private ResultBody() {}

    public static<T> ResultBody success(T obj) {
        ResultBody body = new ResultBody();
        if (Objects.isNull(obj)) {
            body.setCode(ErrorCodeEnum.SUCCESS.getCode());
            body.setMsg("没有数据");
            body.setData(null);
            return body;
        } else {
            body.setCode(ErrorCodeEnum.SUCCESS.getCode());
            body.setMsg(ErrorCodeEnum.SUCCESS.getMsg());
            body.setData(obj);
            return body;
        }
    }

    public static ResultBody fail() {
        ResultBody body = new ResultBody();
        body.setData(null);
        body.setCode(ErrorCodeEnum.FAIL.getCode());
        body.setMsg(ErrorCodeEnum.FAIL.getMsg());
        return body;
    }

    public static ResultBody fail(ErrorCodeEnum codeEnum) {
        ResultBody body = new ResultBody();
        body.setData(null);
        body.setCode(codeEnum.getCode());
        body.setMsg(codeEnum.getMsg());
        return body;
    }

    public static ResultBody fail(String code, String msg) {
        ResultBody body = new ResultBody();
        body.setData(null);
        body.setCode(code);
        body.setMsg(msg);
        return body;
    }

    public static ResultBody empty() {
        ResultBody body = new ResultBody();
        body.setData(null);
        body.setCode(ErrorCodeEnum.RESULT_EMPTY.getCode());
        body.setMsg(ErrorCodeEnum.RESULT_EMPTY.getMsg());
        return body;
    }

    public static ResultBody status(boolean b) {
        return b ? success("操作成功") : fail();
    }

    public static <T>ResultBody list(List<T> list) {
        return list.isEmpty() ? empty() : success(list);
    }
}
